﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Exhibition.Dao.ViewModel;
using AutoMapper;
namespace Exhibition.Dao
{

    public class FuncDao
    {


        public FuncDao()
        {

            Mapper.CreateMap<E_Func, FuncModel>();
        }
        public List<E_Func> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                recordcount = edm.E_Func.Count();

                return edm.E_Func.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }
        /// <summary>
        /// 根据pid获取子列表
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public List<FuncModel> GetListByPid(int pid)
        {
            using (ExhibitionEntities edm = new ExhibitionEntities())
            {
                var query = edm.E_Func.Where(s => s.ParentId == pid);
                return Mapper.Map<List<E_Func>, List<FuncModel>>(query.ToList());
            }

        }
        /// <summary>
        /// 根据funcode获取子列表
        /// </summary>
        /// <param name="funcode"></param>
        /// <returns></returns>
        public List<FuncModel> GetListByFunCode(string funcode)
        {
            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                if (!string.IsNullOrEmpty(funcode))
                {
                    var pmodel = edm.E_Func.FirstOrDefault(s => s.FuncCode == funcode);
                    if (pmodel != null)
                    {
                        var query = edm.E_Func.Where(s => s.ParentId == pmodel.Id);
                        return Mapper.Map<List<E_Func>, List<FuncModel>>(query.ToList());
                    }
                }
                else {
                    var query = edm.E_Func.Where(s => s.ParentId == 0);
                    return Mapper.Map<List<E_Func>, List<FuncModel>>(query.ToList());
                }
                return null;
            }
        }




        public FuncModel GetOneById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Func.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<E_Func, FuncModel>(query);
            }

        }


        public int Save(FuncModel obj)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Func.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new E_Func();

                    edm.AddToE_Func(query);

                }


                query.FuncName = obj.FuncName;

                query.FuncPath = obj.FuncPath;

                query.OrderId = obj.OrderId;

                query.IsDelete = obj.IsDelete;

                query.ParentId = obj.ParentId;
                query.Icon = obj.Icon;
                edm.SaveChanges();

                return query.Id;


            }

        }


        public int DelById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Func.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

